

import LayerManager from "./LayerManager"
import EventDispatcher from "../event/EventDispatcher";
import GlobalEvent from "../event/GlobalEvent";
import LayerInterface from "./LayerInterface";
import { logInfo, UIIndex } from "../tools/Define";
import SingleManager from "./SingleManager";
import StackLayerManager from "./StackLayerManager";
import QueueLayerManager from "./QueueLayerManager";




export default class UIManager extends EventDispatcher {


    private managers: LayerManager[] = [];

    private root: any;

    private static ins: UIManager;

    static instance(): UIManager {
        if (!UIManager.ins) {
            UIManager.ins = new UIManager();
        }
        return UIManager.ins;
    }

    getRoot() {
        return this.root;
    }


    constructor() {
        super();
        this.managers = [];
    }


    /**
     * 隐藏底层UI,减少显示对象个数
     * @param uiIndex 
     * @param flag 
     */
    setVisible(uiIndex: UIIndex, flag: boolean) {
        // logInfo('setVisible ', uiIndex, flag)
        this.managers[uiIndex].setVisible(flag)
    }

    init(node: any) {
        this.root = node
        this.setManager(UIIndex.ROOT, new SingleManager(1, false))
        this.setManager(UIIndex.STACK, new StackLayerManager(10, true))
        this.setManager(UIIndex.QUEUE, new QueueLayerManager(100, true))
        this.setManager(UIIndex.TOAST, new StackLayerManager(200, false))
        this.setManager(UIIndex.TOP, new StackLayerManager(300, false))
    }


    setManager(index: UIIndex, manager: LayerManager) {
        this.managers[index] = manager;
        this.managers[index].init(this.root)
    }




    /**
     * 清除UI
     */
    clear() {
        for (let index = this.managers.length - 1; index >= 0; index--) {
            const manager = this.managers[index];
            if (manager.canPop() && manager.count() > 0) {
                manager.clear()
            }
        }
    }

    /**
     * 用于在启动页的时候直接将节点放入管理器。实际场景中
     * 已经加载了该节点。
     * @param layer 
     * @param uiIndex 
     */
    addView(layer: LayerInterface, uiIndex: UIIndex) {
        if (layer) {
            let manager = this.managers[uiIndex];
            if (!manager) {
                console.log(' manager is null ', layer)
                return;
            }
            if (manager.isValid()) {
                manager.addView(layer);
            }
        }
    }

    //添加UI
    pushView(layer: LayerInterface) {
        if (layer) {
            let uiIndex = layer.getUIIndex()
            let manager = this.managers[uiIndex];
            if (!manager) {
                console.log(' manager is null ', layer)
                return;
            }
            if (manager.isValid()) {
                layer.setUIIndex(uiIndex)
                manager.pushView(layer);
                this.getOpenUICount();
            }

        }
    }



    /**
     * 此方法用于关闭界面，为了兼容Android的back键 所以layer有为null的情况。
     * 如果 返回false 表明已经没有界面可以弹出
     * @param layer 
     */
    popView(layer?: LayerInterface) {
        // console.log('popView layer is ', layer)
        let flag = false;
        if (layer) {
            let index: number = layer.getUIIndex()
            // console.log(' popView  index is ', index, ' layer is ', layer)
            let manger = this.managers[index];
            if (!manger) {
                // console.log(' popView layer is not found type is ', type)
                flag = false;
            } else {
                flag = manger.popView(layer);
            }
        } else {
            for (let index = this.managers.length - 1; index >= 0; index--) {
                const manager = this.managers[index];
                if (manager.canPop() && manager.count() > 0) {
                    if (manager.popView(null)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        GlobalEvent.instance().emit(GlobalEvent.POP_VIEW, this.getOpenUICount())
        return flag;

    }


    getOpenUICount() {
        let count: number = 0;
        for (let index = this.managers.length - 1; index >= 0; index--) {
            const manager = this.managers[index];
            if (manager.canPop()) {
                count += manager.count()
            }
        }
        return count;
    }
	
    hasView(name: string, uiIndex: UIIndex = UIIndex.STACK) {
        let manager = this.managers[uiIndex];
        if (manager) {
            return manager.hasView(name)
        }
        return false;
    }
}
